<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form label-width="70px" size="small">
          <el-row :gutter="10">
            <el-col :lg="6" :md="8" :sm="12" :xl="6" :xs="24">
              <el-form-item label="名称">
                <el-input
                    v-model="query.name"
                    class="filter-item"
                    clearable
                    placeholder="输入客户名称搜索"
                    size="small"
                    style="width: 200px;"
                    @keyup.enter.native="crud.toQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="8" :sm="12" :xl="6" :xs="24">
              <el-form-item label="归属上级">
                <el-input
                    v-model="query.pname"
                    class="filter-item"
                    clearable
                    placeholder="输入归属上级名称搜索"
                    size="small"
                    style="width: 200px;"
                    @keyup.enter.native="crud.toQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="8" :sm="12" :xl="6" :xs="24">
              <el-form-item label="编码">
                <el-input
                    v-model="query.code"
                    class="filter-item"
                    clearable
                    placeholder="输入客户编码搜索"
                    size="small"
                    style="width: 200px;"
                    @keyup.enter.native="crud.toQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="8" :sm="12" :xl="6" :xs="24">
              <el-form-item label="状态">
                <el-select v-model="query.status" class="edit-input" placeholder="请选择" style="width: 100%;">
                  <el-option
                      v-for="(item,index) in dict.common_status"
                      :key="index"
                      v-if="item.seq>0"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <rrOperation :crud="crud"/>
      </div>

      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :before-close="crud.cancelCU" :close-on-click-modal="false" :title="crud.status.title"
                 :visible.sync="crud.status.cu > 0" width="500px" @closed="disableEditTree=false">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="归属上级">
            <treeselect v-model="form.pid" :disabled="disableEditTree" :normalizer="normalizer" :options="treeList"
                        placeholder="选择归属上级" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number v-model="form.seq" :min="1" label="排序" style="width: 370px;"></el-input-number>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.status" class="edit-input" placeholder="请选择" style="width: 370px;">
              <el-option
                  v-for="(item,index) in dict.common_status"
                  :key="index"
                  v-if="item.seq>0"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" type="textarea"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="body-container">

      <vue-context-menu :contextMenuData="contextMenuData"
                        @tableConfig="tableConfig"
                        @deletedata="deletedata"></vue-context-menu>
      <!-- 表格配置 -->
      <el-dialog title="表格配置"
                 :visible.sync="tableConfigShow"
                 width="60%">
        <j-table-column-config :columns.sync="crud.header"  @tabSave="tabSave" @pit="pit" />
      </el-dialog>

      <!--表格渲染-->

      <el-table ref="table"  border
                height="700"
                show-summary
                @cell-dblclick="row=>crud.toEdit(row)"
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                v-if="isShowTable"
                @header-dragend="changeColWidth"
                @contextmenu.native="showMenu"
                :summary-method="crud.sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler"
                :tree-props="{children: 'children', hasChildren: 'children.length>0'}"
                border row-key="id" >
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" label="序号" type="index" width="60"/>
        <el-table-column
          v-if="item.visible"
          v-for="(item, index) in crud.header"
          :key="`col_${index}`"
          :prop="item.prop"
          :label="columnLabel(item)"
          :width="item.width + 'px'"
          :data-porp='item.prop'
          @contextmenu.native="showMenu"
          @header-dragend="changeColWidth"
        >
          <!-- 自定义渲染模版 -->
          <template slot-scope="scope">
            <!-- 当列key为 sort 时，渲染一个手柄icon -->
            <template v-if="item.prop =='sort'">
              <div class="handle_move">
                <i :class="scope.row['sort']" />
              </div>
            </template>
            <template v-else>
              <span v-if="item.type == 1" style="margin-left: 0px">{{scope.row[`${item.prop}`]}}</span>
              <span v-if="item.type == 2" style="margin-left: 0px">{{ parseTime(scope.row[`${item.prop}`])}}</span>
              <span v-if="item.type == 3" style="margin-left: 0px">{{ getDict(`this.dict.label.${item.dictionary}["${scope.row[item.prop]}"]`) }}</span>
            </template>
          </template>
        </el-table-column>
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" label="编码" prop="code">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.pcode ? (scope.row.pcode + ' / ' + scope.row.code) : scope.row.code }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" label="名称" prop="name">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.pname ? (scope.row.pname + ' / ' + scope.row.name) : scope.row.name }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('pname')" label="上级名称" prop="pname"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" label="状态" prop="status">-->
<!--          <template slot-scope="scope">-->
<!--            {{ dict.label.common_status[scope.row.status] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" label="备注" prop="remark"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','basCustomerArea:edit','basCustomerArea:del']" align="center" label="操作"-->
<!--                         width="200px">-->
<!--          <template slot-scope="scope">-->
<!--            <udOperation-->
<!--                :data="scope.row"-->
<!--                :permission="permission"-->
<!--            >-->
<!--              <el-tooltip slot="right" class="item" content="添加下级" effect="light" placement="top">-->
<!--                <i class="el-icon-plus opration_icon_btn" style="margin-right: 14px"-->
<!--                   @click="addChildren(scope.row)"/>-->
<!--              </el-tooltip>-->
<!--            </udOperation>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination layout='prev, pager, next, sizes'/>
    </div>
  </div>
</template>

<script>
import crudBasCustomerArea, {getCustomerAreaTree} from '@/api/erp/basCustomerArea'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'

import { publicListKey } from "@/utils/key";
import {getHeaderProperties} from "@/api/erp/headerProperties";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '客户区域',
  url: 'api/basCustomerArea',
  sort: 'code,asc',
  crudMethod: {...crudBasCustomerArea}
})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  pid: null,
  pcode: null,
  pname: null,
  seq: null,
  status: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'BasCustomerArea',
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect,jTableColumnConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: ['t_bas_subject_type', 'common_status','t_bas_tax_type'],
  data() {
    return {

      permission: {
        add: ['admin', 'basCustomerArea:add'],
        edit: ['admin', 'basCustomerArea:edit'],
        del: ['admin', 'basCustomerArea:del'],
        import: ['admin', 'basCustomerArea:import'],
        export: ['admin', 'basCustomerArea:export'],
        copy: ['admin', 'basCustomerArea:copy'],
        upload: ['admin', 'basCustomerArea:upload'],
        download: ['admin', 'basCustomerArea:download'],
        enable: ['admin', 'basCustomerArea:enable'],
        disable: ['admin', 'basCustomerArea:disable'],
      },
      rules: {  name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '编码不能为空', trigger: 'blur'}
        ],},
      treeList: [],
      defaultProps: {children: 'children', label: 'name', code: 'code'},
      disableEditTree: false
    }
  },
  watch: {},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      //获取树型数据
      getCustomerAreaTree().then(res => {
        // this.treeList  = res.data
        this.treeList = []
        const treeList = {id: 0, name: '顶级分类', children: []}
        treeList.children = res.data
        this.treeList.push(treeList)

      })

    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    addChildren(data) {
      //添加下级项目
      let _form = JSON.parse(JSON.stringify(defaultForm))
      _form.pid = data.id
      this.crud.resetForm(_form);
      this.disableEditTree = true
      this.crud.toAdd()
    }
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
